package com.ldh.jedis;

import com.ldh.jedis.resp.ClientEndecode;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @program: jedis1
 * @description:
 * @author: Donghui Li
 * @create: 2021-12-08 17:02
 */
public class Client {

    private static String host = "127.0.0.1";
    private static int port = 6379;

    public static void main(String[] args) {
        try {
            Socket socket = new Socket(host, port);
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            System.out.print(host+":"+port+">");
            Scanner input = new Scanner(System.in);
            while(input.hasNext()){
                String command = input.nextLine();      //接受用户输入
                String s = ClientEndecode.ArrayStringEncode(command);
                bufferedWriter.write(s);
                bufferedWriter.flush();
                String decode = ClientEndecode.decode(bufferedReader);
                System.out.println(decode);
                System.out.print(host+":"+port+">");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}